package com.example.shiwu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.shiwu.adapter.CvsAdapter;
import com.example.shiwu.db.model.CvsBean;
import com.example.shiwu.db.model.Sends;
import com.example.shiwu.manager.DynamicLineChartManager;
import com.example.shiwu.service.SendsService;
import com.example.shiwu.util.DateUtils;
import com.example.shiwu.util.FileDownUtil;
import com.example.shiwu.util.TitleSettingUtil;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SendReceiveDetailsActivity extends AppCompatActivity {
    private DynamicLineChartManager dynamicLineChartManager;
    private List<Integer> list = new ArrayList<>(); //数据集合
    private List<String> names = new ArrayList<>(); //折线名字集合
    private List<Integer> colour = new ArrayList<>();//折线颜色集合
    public TextView title_text, title_setting;
    public ImageView title_back;

    private static final String TAG = MainActivity.class.getSimpleName();

    private ListView mRecyclerView;

    private List<CvsBean> mList;
    private SendsService sendService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_receive_details);
        title_back = findViewById(R.id.title_back);
        title_text = findViewById(R.id.title_text);
        title_setting = findViewById(R.id.title_setting);
        mRecyclerView = findViewById(R.id.send_detail_list_view);
        TitleSettingUtil.initTitle("详情",null,title_back,title_text,title_setting);
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendReceiveDetailsActivity.this.finish();
            }
        });
        sendService = new SendsService();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        Sends sends = new Sends();
        sends.setId(Integer.parseInt(id));
        List<Sends> sendslis = sendService.getSends(sends);
        String uid = sendslis.get(0).getUid();
        String itemid = String.valueOf(sendslis.get(0).getItemId());
        String fname = itemid+"-"+uid+".cvs";
        readCsvFile(fname);
    }

    private void readCsvFile(String fileName) {
        mList = readCsv(FileDownUtil.DOWN_NAME + fileName);

        if (mList.size() > 0) {
            System.out.println("Csv 文件读取成功");
        } else {
            System.out.println("Csv 文件读取失败");        }

        CvsAdapter mAdapter = new CvsAdapter(this,mList,R.layout.send_details_list_item_cvs);

        mRecyclerView.setAdapter(mAdapter);
        DateUtils.setListViewHeightBasedOnChildren(mRecyclerView);//解决只显示一条数据的问题 动态计算高度
    }

    private ArrayList<CvsBean> readCsv(String path) {
        ArrayList<CvsBean> readerArr = new ArrayList<>();
        File file = new File(path);
        FileInputStream fileInputStream;
        Scanner in;
        try {
            fileInputStream = new FileInputStream(file);
            in = new Scanner(fileInputStream, "UTF-8");
            in.nextLine();
            while (in.hasNextLine()) {
                String[] lines = in.nextLine().split(",");
                CvsBean bean = new CvsBean(lines[0], lines[1], lines[2],lines[3], lines[4], lines[5],lines[6], lines[7], lines[8],lines[9]);
                readerArr.add(bean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return readerArr;
    }

}